﻿#include "rulerwidget.h"

RulerWidget::RulerWidget(QWidget *parent) : QWidget(parent)
{
    pCommonParameters = new struct _commons;
}

RulerWidget::~RulerWidget()
{

}


void RulerWidget::setScale(int scale)
{
    this->scale = scale;
    this->update();

    return;
}

void RulerWidget::setCommonParameters(pCOMMONS pCommons)
{
    pCommonParameters = pCommons;

    return;
}


void RulerWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);

//    // 画横线
//    p.drawLine(0, 0, this->width(),0);

    // 画刻度线
    for(int i = 0; i< MINOR_SCALE_NUMBERS + 1; ++i)
    {
        // 大刻度
        if(i % (MINOR_SCALE_NUMBERS / MAJOR_SCALE_NUMBERS) == 0)
        {
//            p.drawLine(i * (this->width() / MINOR_SCALE_NUMBERS), 0, i * (this->width() / MINOR_SCALE_NUMBERS), (pCommonParameters->RULER_AREA_HEIGHT / 3));
            // 标注尺度
            QString show_scale = QString::number(scale * ( MINOR_SCALE_NUMBERS / MAJOR_SCALE_NUMBERS) * (i / (MINOR_SCALE_NUMBERS / MAJOR_SCALE_NUMBERS) - (MAJOR_SCALE_NUMBERS + 1) / 2), 10);
            QFontMetrics fm = QFontMetrics(this->font());
            QRect rect = fm.boundingRect(show_scale);

            if(i == 0)
            {
                p.drawText(i * ((float)this->width() / MINOR_SCALE_NUMBERS), (float)pCommonParameters->RULER_AREA_HEIGHT / 2, show_scale);
//                p.drawText(i * ((float)this->width() / MINOR_SCALE_NUMBERS), ((float)pCommonParameters->RULER_AREA_HEIGHT / 3) * 2 + 10, show_scale);
                continue;
            }
            if(i == MINOR_SCALE_NUMBERS)
            {
                p.drawText(i * ((float)this->width() / MINOR_SCALE_NUMBERS) - (float)rect.width(), (float)pCommonParameters->RULER_AREA_HEIGHT / 2, show_scale);
//                p.drawText(i * ((float)this->width() / MINOR_SCALE_NUMBERS) - (float)rect.width(), ((float)pCommonParameters->RULER_AREA_HEIGHT / 3) * 2 + 10, show_scale);
                continue;
            }

            p.drawText(i * ((float)this->width() / MINOR_SCALE_NUMBERS) - (float)rect.width() / 2, (float)pCommonParameters->RULER_AREA_HEIGHT / 2, show_scale);
//            p.drawText(i * ((float)this->width() / MINOR_SCALE_NUMBERS) - (float)rect.width() / 2, ((float)pCommonParameters->RULER_AREA_HEIGHT / 3) * 2 + 10, show_scale);
            continue;

        }

        // 小刻度
//        p.drawLine(i * (this->width() / MINOR_SCALE_NUMBERS), 0, i * (this->width() / MINOR_SCALE_NUMBERS), (pCommonParameters->RULER_AREA_HEIGHT / 6));

    }

    return;

}


